import scriptlerweb.Script
import scriptlerweb.Provider

def action = params?.action

switch(action){
	
	case 'rate':
		def id = params.id
		def rating = params.rating
		if(rating && id){
			def script = dao.loadScript(id)
			if("pos".equals(rating)){
				script.ratingPos++
				dao.ofy().put(script)
			}else if("neg".equals(rating)){
				script.ratingNeg++
				dao.ofy().put(script)
			}
		}
		out.write 'successfully rated'
	
		break
	case 'download':
		def id = params.id
		if(!id){
			request.setAttribute 'msg', "no script id given!"
			forward '/catalog/list'
		}else{
			def script = dao.loadScript(id)
			script.downloadCount++
			dao.ofy().put(script)
			
			log.fine "write script to out"
			out.write script.scriptSrc
		}
	
		break
	case 'new':
		def provider = dao.loadCurrentProvider(user)
		if(!provider){
			redirect "/notauth.gtpl"
		}else if(!provider.name){
			request.setAttribute 'msg', "finish your registration"
			request.setAttribute 'provider', provider
			forward "/provider/edit/$provider.id"
		}else{
			log.fine "forward to new script"
			def script = new Script()
			request.setAttribute 'script', script
			forward '/scriptedit.gtpl'
		}
	
		break
	case 'edit':
		def provider = dao.loadCurrentProvider(user)
		if(!provider){
			redirect "/notauth.gtpl"
		}else{
			def id = params.id
			if(!id){
				request.setAttribute 'msg', "no script id given!"
				forward '/catalog/list'
			}else{
				log.fine "Search for script with ID: $id"
				def script = dao.loadScript(id)
				request.setAttribute 'script', script
				forward '/scriptedit.gtpl'
			}
		}
	
		break	
	case 'show':
		def id = params.id
		if(!id){
			request.setAttribute 'msg', "no script id given!"
			forward '/catalog/list'
		}else{
			log.fine "Search for script with ID: $id"
			def script = dao.loadScript(id)
			if(script){
				def p = dao.loadProvider(script.providerId)
				script.providerName = p.name
				request.setAttribute 'script', script
				forward '/scriptdetails.gtpl'
			}else{
				// would be good to display a message, buld right now it is not possible to forward a message to an action 
				// request.setAttribute 'msg', "we are sorry, but a script with id [$id] could not be found"
				forward '/catalog/list'
			}
		}
	
		break
	case 'save':
		def provider = dao.loadCurrentProvider(user)
		if(!provider){
			redirect "/notauth.gtpl"
		}else{
			log.fine "save/edit script with provider: $provider"
			if(params.id){
				def script = dao.loadScript(params.id)
				if(provider){
					log.fine "*** ${provider.id} - ${script.providerId}: "+provider.id.equals(script.providerId)
					if(provider.id.equals(script.providerId)){
						script.name = params.name
						script.comment = params.comment
						script.scriptSrc = params.scriptSrc
						script.lang = params.lang
						script.lastUpdate = new Date()
						dao.ofy().put(script)
						request.setAttribute 'script', script
					}else{
						request.setAttribute 'msg', "your not allowed to edit this script!"
					}
				}
				redirect "/script/show/${params.id}"
			}else{
				log.fine "save script with provider: $provider"
				if(provider){
					if(provider && provider.id){
						if(params.name && params.scriptSrc){
							def s = new Script(name: params.name, comment: params.comment,scriptSrc: params.scriptSrc, lang: params.lang, lastUpdate: new Date())
							s.providerId = provider.id
							dao.ofy().put(s)
							redirect "/script/show/${s.id}"
						}else{
							request.setAttribute "msg", "not all required fields set!"
							def s = new Script(name: params.name, comment: params.comment,scriptSrc: params.scriptSrc, lang: params.lang)
							request.setAttribute 'script', s
							forward '/scriptedit.gtpl'
						}
					}
				}
			}
		}
	
		break
	default:
		redirect '/catalog/list'
}
